import logging
from qiniu import Auth, put_data
# 需要填写你的 Access Key 和 Secret Key
access_key = "ICOkaltiDj515AE-BQv8Y-uV41_b2FVmbtFztU-3"
secret_key = "apf-cwpR89iFBNjZhJjnswR84ba1XyjRZn6ejs01"
# 要上传的空间
bucket_name = 'haha'
def storage(data):
    """七⽜云存储上传⽂件接⼝"""
    if not data:
        return None
    try:
        # 构建鉴权对象用户头像上传144
        q = Auth(access_key, secret_key)
        # ⽣成上传 Token，可以指定过期时间等
        token = q.upload_token(bucket_name)
        # 上传⽂件
        ret, info = put_data(token, None, data)
        print(info)
    except Exception as e:
        logging.error(e)
        raise e
    if info and info.status_code != 200:
        raise Exception("上传⽂件到七⽜失败")
        # 返回七⽜中保存的图⽚名，这个图⽚名也是访问七⽜获取图⽚的路径
    return ret["key"]
if __name__ == '__main__':
    file = input('请输⼊⽂件路径')
    with open(file, 'rb') as f:
        storage(f.read())
